﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity;
using System.Data;
using Microsoft.SharePoint;
using JM.JCY.BusinessEntity.PPSM2007;
using JM.JCY.Proxy;
using System.Linq;

namespace JM.JCY.SharePointComponent.PersonalHome.PersonalAwardPublic
{
    public partial class PersonalAwardPublicUserControl : UserControl
    {
        PPSM2007CodeDataSet.T_PPSM2007CODEDataTable CodeTable;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CodeTable = GetPPSM2007CODEData();

                if (Request.QueryString["StaffId"] != null)
                {
                    int _staffId = Int32.Parse(Request.QueryString["StaffId"].ToString());
                    using (UserManagementProxy proxy = new UserManagementProxy())
	                {
                        string currentUserName = proxy.GetUser(_staffId,string.Empty)[0].UserName;
                        DataRow[] awardRows = GetAwardInfoByName(currentUserName).Select();
                        dlAward.DataSource = awardRows;
                        dlAward.DataBind();
                    }
                }
            }

        }
        protected void dlAward_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserAssessmentDataSet.UserGloryRow row = (PPSMUserAssessmentDataSet.UserGloryRow)e.Item.DataItem;
            Label lbAwardDate = (Label)e.Item.FindControl("lbAwardDate");
            Label lbAwardName = (Label)e.Item.FindControl("lbAwardName");
            Label lbAwardPlaceName = (Label)e.Item.FindControl("lbAwardPlaceName");
            lbAwardDate.Text = string.IsNullOrEmpty(row.GloryDate) ? string.Empty : row.GloryDate.Substring(0, 4).ToString() + "年" + row.GloryDate.Substring(4, 2).ToString() + "月" + row.GloryDate.Substring(6, 2).ToString() + "日";
            lbAwardName.Text = string.IsNullOrEmpty(row.GloryArea) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "奖励范围", row.GloryArea);
            lbAwardPlaceName.Text = string.IsNullOrEmpty(row.GloryByUnitName) ? string.Empty : row.GloryByUnitName;
        }
        public PPSMUserAssessmentDataSet.UserGloryDataTable GetAwardInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GeGloryInfoByName(userName);
            }
        }


        public string GetCodeDescByCategoryAndValue(PPSM2007CodeDataSet.T_PPSM2007CODEDataTable dt, string category, string value)
        {
            var codeDesc = from codeTable in dt
                           where codeTable.Category == category && codeTable.CodeValue == value
                           select codeTable.CodeDesc;
            return codeDesc.First();


        }

        public PPSM2007CodeDataSet.T_PPSM2007CODEDataTable GetPPSM2007CODEData()
        {
            var dt = new PPSM2007CodeDataSet.T_PPSM2007CODEDataTable();
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetPPSM2007CodeTable();
            }
        }
    }
}
